package ganga.custodian.settings;

import ganga.custodian.BluetoothManager;
import ganga.custodian.CustodianApplication;
import ganga.custodian.R;
import ganga.custodian.ui.Builder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import java.util.ArrayList;

/**
 * Пользовательский интерфейс для определения параметров кардиомонитора:
 * 
 * @author Apocrypha
 * 
 */
public class CardioMonitorParameters extends Activity {
	private Builder builder = new Builder(this);
	ArrayList<String> devicesNames;
	ArrayList<String> devicesAddresses;//"00:11:22:AA:BB:CC"
	CustodianApplication application = CustodianApplication.getInstance();
	BluetoothManager manager = application.getBluetoothManager();
	/**
	 * Строка, с адресом выбранного устройства 
	 * Например "00:11:22:AA:BB:CC"
	 */
	protected String selectedDevice;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cardiomonitorparameters);
		
		builder.buildTextView(R.id.cardioMonitorTypeValueTextView, application.cardioMonitor);
		
		devicesNames = new ArrayList<String>();
		devicesAddresses = new ArrayList<String>();
		manager.populateDevicesList(devicesNames, devicesAddresses);

		builder.buildSpinner(R.id.selectBluetoothDeviceSpinner, devicesNames);
		buildSpinnerListener(R.id.selectBluetoothDeviceSpinner);
		
		Button aButton = (Button) findViewById(R.id.connectButton);
		aButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				application.bluetoothManager.connect(selectedDevice);
			}
		});

	}
	@Override
	protected void onResume() {
		super.onResume();
		BluetoothManager.adapter.enable();
	}
	private void buildSpinnerListener(int spinnerId) {
		Spinner spinner = (Spinner) this.findViewById(spinnerId);

		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id) {
				selectedDevice = devicesAddresses.get(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parentView) {
				// Nothing to do
				application.showToast("Nof"+ selectedDevice);
			}
		});
	}
}